package net.spatula.tally_ho.ui.article.common;

/* Copyright 2006 Nicklas R Johnson
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

import java.util.List;

import javax.persistence.EntityManager;

import net.spatula.tally_ho.jpa.ResourceManager;
import net.spatula.tally_ho.service.SectionService;
import net.spatula.tally_ho.service.beans.ArticleBean;
import net.spatula.tally_ho.service.beans.SectionBean;
import net.spatula.tally_ho.ui.behaviors.DropDownLabelUpdateBehavior;
import net.spatula.tally_ho.ui.validators.HtmlCleanValidator;
import net.spatula.tally_ho.ui.validators.NoHtmlValidator;
import net.spatula.tally_ho.ui.validators.TrueValidator;
import wicket.markup.html.basic.Label;
import wicket.markup.html.form.CheckBox;
import wicket.markup.html.form.ChoiceRenderer;
import wicket.markup.html.form.DropDownChoice;
import wicket.markup.html.form.Form;
import wicket.markup.html.form.FormComponent;
import wicket.markup.html.form.TextArea;
import wicket.markup.html.form.TextField;
import wicket.markup.html.form.validation.StringValidator;
import wicket.markup.html.panel.ComponentFeedbackPanel;
import wicket.model.PropertyModel;

public class ArticleForm extends Form {

    private static final long serialVersionUID = 1L;
    private ArticleBean article;
    private Label sectionDescription = new Label("sectionDescription", "");
    private Class lastPage;

    public ArticleForm(final String componentName, Class lastPage) {
        super(componentName);
        
        this.lastPage = lastPage;
        
        article = ((ArticleSession)getSession()).getArticle();

        FormComponent body = new TextArea("body", new PropertyModel(article, "body")).setRequired(true).add(
                StringValidator.lengthBetween(4, 49152));
        body.add(new HtmlCleanValidator());
        add(body);

        FormComponent summary = new TextArea("summary", new PropertyModel(article, "summary")).setRequired(true).add(
                new NoHtmlValidator()).add(StringValidator.lengthBetween(4, 320));
        add(summary);

        FormComponent adjective = new TextField("adjective", new PropertyModel(article, "adjective"))
                .add(new NoHtmlValidator());
        add(adjective);

        TextField headline = new TextField("headline", new PropertyModel(article, "headline"));
        headline.add(new NoHtmlValidator());
        headline.setRequired(true);
        headline.add(StringValidator.lengthBetween(1, 80));
        add(headline);

        EntityManager em = ResourceManager.getInstance().getEMF().createEntityManager();
        List<SectionBean> sectionsList = SectionService.getInstance().getSections(em);
        em.close();

        sectionDescription.setOutputMarkupId(false); // absolutely don't want
        // it overwritten
        if (article.getSection() != null && article.getSection().getId() != 0) {
            sectionDescription.setModelObject(article.getSection().getDescription());
        }
        add(sectionDescription);

        DropDownChoice sectionChooser = new DropDownChoice("section", new PropertyModel(article, "section"), sectionsList,
                new ChoiceRenderer("name", "id"));
        sectionChooser.add(new DropDownLabelUpdateBehavior(sectionsList, "sectionDescription", "id", "description"));
        sectionChooser.setRequired(true);

        add(sectionChooser);
               
        FormComponent agree = new CheckBox("agree", new PropertyModel(article, "agreedToLicense")).add(new TrueValidator());
        add(agree);

        add(new ComponentFeedbackPanel("headlineErrors", headline));
        add(new ComponentFeedbackPanel("sectionErrors", sectionChooser));
        add(new ComponentFeedbackPanel("summaryErrors", summary));
        add(new ComponentFeedbackPanel("adjectiveErrors", adjective));
        add(new ComponentFeedbackPanel("bodyErrors", body));
        add(new ComponentFeedbackPanel("agreeErrors", agree));

    }

    public void onSubmit() {
        ((ArticleSession)getSession()).setArticle(article);
        setResponsePage(new PreviewBeforeSubmit(lastPage));
    }

}
